﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Zeta.Bot;
using Zeta.Bot.Settings;
using Zeta.Common;
using Zeta.Game;
using Zeta.Game.Internals;
using Zeta.Game.Internals.Actors;

namespace KadalaSpree.XCore
{
    public static class XVendor
    {

        #region Properties

        /// <summary>
        /// Any vendor window is currently open
        /// </summary>
        public static bool IsVendorWindowOpen
        {
            get { return UIElements.VendorWindow != null && UIElements.VendorWindow.IsValid && UIElements.VendorWindow.IsVisible; }
        }

        #endregion

        #region Methods

        public static bool Purchase(int dynamicId)
        {
            if (dynamicId == 0) return false;

            //var beforeBackpackItems = XInventory.BackPackItems;
            //var beforeShards = XPlayer.BloodShards;            
                
            ZetaDia.Me.Inventory.BuyItem(dynamicId);

            //Thread.Sleep(250);

            //X.Logger.Log("bshards={0} ashards={1} bitems={2} aitems={3}",
            //    beforeShards, XPlayer.BloodShards,
            //    beforeBackpackItems, XInventory.BackPackItems
            //    );

            //using (ZetaDia.Memory.AcquireFrame(true))
            //{
            //    X.Logger.Log("bshards={0} ashards={1} bitems={1} aitems={2}",
            //        beforeShards, XPlayer.BloodShards,
            //        beforeBackpackItems, XInventory.BackPackItems
            //        );
            //}            

            //return false;
            return true;
        }

        /// <summary>
        /// Returns true if our bags have been emptied
        /// </summary>
        public static bool IsEmptyBackpack
        {
            get
            {
                // There should be nothing but potions in the backpack after town run is complete. 
                var nonPotions = ZetaDia.Me.Inventory.Backpack.Where(i => i.ItemType != ItemType.Potion).ToList();
                var nonPotionCount = 0;

                if (nonPotions.Any())
                {
                    nonPotionCount = nonPotions.Count();

                    var backpackSlotBlocked = new bool[10, 6];

                    foreach (InventorySquare square in CharacterSettings.Instance.ProtectedBagSlots)
                    {
                        backpackSlotBlocked[square.Column, square.Row] = true;
                    }

                    foreach (var item in nonPotions)
                    {
                        int row = item.InventoryRow;
                        int col = item.InventoryColumn;

                        if (backpackSlotBlocked[col, row])
                        {
                            nonPotionCount--;
                        }
                    }

                }
                return (nonPotionCount <= 0);
            }
        }

        public static VendorSlotType GetVendorSlotType(DiaItem item)
        {
            return GetVendorSlotType(item.CommonData.ItemBaseType, item.CommonData.ItemType, item.CommonData.IsTwoHand, item.CommonData.IsOneHand);
        }

        public static VendorSlotType GetVendorSlotType(ItemBaseType itemBaseType, ItemType itemType, bool twoHand, bool oneHand)
        {
            switch (itemType)
            {
                case ItemType.Amulet:
                    return VendorSlotType.Amulet;
                case ItemType.Ring:
                    return VendorSlotType.Ring;
                case ItemType.Helm:
                case ItemType.SpiritStone:
                case ItemType.VoodooMask:
                case ItemType.WizardHat:
                    return VendorSlotType.Helm;
                case ItemType.Belt:
                case ItemType.MightyBelt:
                    return VendorSlotType.Belt;
                case ItemType.Shoulder:
                    return VendorSlotType.Shoulder;
                case ItemType.Cloak:
                case ItemType.Chest:
                    return VendorSlotType.Chest;
                case ItemType.Legs:
                    return VendorSlotType.Pants;
                case ItemType.Boots:
                    return VendorSlotType.Boots;
                case ItemType.Mojo:
                    return VendorSlotType.Mojo;
                case ItemType.Orb:
                    return VendorSlotType.Orb;
                case ItemType.Quiver:
                    return VendorSlotType.Quiver;
                case ItemType.Shield:
                case ItemType.CrusaderShield:
                    return VendorSlotType.Shield;
            }

            if (itemBaseType == ItemBaseType.Weapon)
            {
                if (twoHand)
                {
                    return XCore.VendorSlotType.TwoHandItem;
                }
                if (oneHand)
                {
                    return XCore.VendorSlotType.OneHandItem;
                }
            }

            return VendorSlotType.None;
        }

        #endregion

    }
}
